package com.hanbang.maige.view.huodong.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hanbang.maige.R;
import com.hanbang.maige.code.base.baseadapter.recyclerview.CommonAdapter;
import com.hanbang.maige.code.base.view.fragment.BaseListFragment;
import com.hanbang.maige.code.broadcast.BroadcastUtils;
import com.hanbang.maige.mode.javabean.huodong.HuoDongItemData;
import com.hanbang.maige.pressenter.huodong.HuoDongPresenter;
import com.hanbang.maige.utils.ui.divider.HorizontalDividerItemDecoration;
import com.hanbang.maige.view.huodong.IHuoDongBaseView;
import com.hanbang.maige.view.huodong.activity.HuoDongDetailActivity;
import com.hanbang.maige.view.huodong.activity.PublishHuoDongActivity;
import com.hanbang.maige.view.huodong.adapter.HuoDongAdapter;
import com.hanbang.maige.widget.empty_layout.ContextData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者　　: 李坤
 * 创建时间:2016/10/17　15:45
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：
 */

public class HuoDongFragment extends BaseListFragment<IHuoDongBaseView.IHuoDongView, HuoDongPresenter, HuoDongItemData>
        implements IHuoDongBaseView.IHuoDongView, HuoDongAdapter.OnButtnClickListener {

    @BindView(R.id.addFab)
    FloatingActionButton addFab;
    PublishHuoDongABroadcast publishHuoDongABroadcast;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                presenter.getHttpData(true);
            }
        }, new IntentFilter(BroadcastUtils.HUODONG_PUBLISH));
        activity.registerReceiver(publishHuoDongABroadcast = new PublishHuoDongABroadcast(), new IntentFilter(BroadcastUtils.HUODONG_PUBLISH));
        presenter.getHttpData(true);
    }

    @Override
    public HuoDongPresenter initPressenter() {
        return new HuoDongPresenter();
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_huodong;
    }

    @Override
    public void initView() {
        super.initView();
        toolbar.addAction(0, "", R.drawable.material_null);
        toolbar.setStatusViewHeight();
    }

    @Override
    public CommonAdapter getAdapter() {
        return adapter = new HuoDongAdapter(getContext(), listDatas, this);
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return new HorizontalDividerItemDecoration.Builder(activity)
                .colorResId(R.color.translucent)
                .build();
    }


    @Override
    public void updataUI(List<HuoDongItemData> result) {
        listDatas.addAll(result);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void baomingSuccess(String msg, int position) {
        MaterialDialog dialog = new MaterialDialog.Builder(getContext())
                .content(msg).title("提示")
                .positiveText("知道了")
                .build();
        dialog.show();
    }

    @Override
    public void onBaoming(int position, String activityId) {
        presenter.postBaoming(activityId, position);
    }

    @Override
    public void onRefresh() {
        presenter.getHttpData(true);
    }

    @Override
    public void onLoadding() {
        presenter.getHttpData(false);
    }

    @Override
    public void onRetryClick(ContextData data) {
        presenter.getHttpData(true);
    }

    @Override
    public void onEmptyClick(ContextData data) {
        presenter.getHttpData(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick(R.id.addFab)
    public void fabuOnClick(View view) {
        PublishHuoDongActivity.startUI(getContext());
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, HuoDongItemData data, int position) {
        HuoDongDetailActivity.startUI(getContext(), data.getActivityID());
    }


    public class PublishHuoDongABroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

        }
    }

    @Override
    public void onDestroy() {
        activity.unregisterReceiver(publishHuoDongABroadcast);
        super.onDestroy();
    }


}
